<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12" v-if="false">
            <a-form-model-item label="价格编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serialNumber">
              <a-input v-model="model.serial" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="false">
            <a-form-model-item label="产品id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productId">
              <a-input v-model="model.materialId"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item
              label="产品代码"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="materialCode"
              :disable="newPrice"
            >
              <HfMaterialSearchSelectTag
                v-model="model.materialCode"
                :material-field="['materialCode', 'materialName', 'sap']"
                :data-url="url.materialList"
                @change="onMaterialChange(arguments)"
              ></HfMaterialSearchSelectTag>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialName">
              <a-input v-model="model.materialName" placeholder="请输入产品名称" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sap">
              <a-input v-model="model.sap" placeholder="请输入规格型号" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <j-dict-select-tag
                v-model="model.unit"
                placeholder="请输入单位"
                dictCode="hf_material_unit,material_unit_name,id"
                disabled
              ></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="物料类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialType">
              <j-dict-select-tag
                v-model="model.materialType"
                placeholder="请输入物料类型"
                dictCode="hf_material_type,material_type_name,id"
                disabled
              ></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单箱数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="perNumber">
              <a-input v-model="model.perNumber" placeholder="请输入单箱数" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="false">
            <a-form-model-item label="仓库" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="warehouse">
              <a-input v-model="model.warehouse" placeholder="请输入仓库" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customId">
              <j-search-select-tag
                type="list"
                v-model="model.customId"
                dict="hf_custom,name,id"
                placeholder="请选择客户名称"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="未税单价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="price">
              <a-input-number
                v-model="model.price"
                placeholder="请输入未税单价"
                style="width: 100%"
                :formatter="value => `￥${value}`"
                :parser="value => value.replace('￥', '')"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="税率" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tax">
              <a-input-number
                v-model="model.tax"
                placeholder="请输入税率"
                style="width: 100%"
                :formatter="value => `${value}%`"
                :parser="value => value.replace('%', '')"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="含税单价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taxPrice">
              <a-input-number v-model="model.taxPrice" style="width: 100%" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="执行时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="useTime">
              <j-date
                placeholder="请选择执行时间"
                v-model="model.useTime"
                :show-time="true"
                date-format="YYYY-MM-DD"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="品番" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="saleCode">
              <a-input v-model="model.saleCode" placeholder="请输入品番" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="简号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="saleCode1">
              <a-input v-model="model.saleCode1" placeholder="请输入简号" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="QAD" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qad">
              <a-input v-model="model.qad" placeholder="请输入QAD" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注"></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { HfIODetailMixin } from '@/mixins/HfIODetailMixin'
import HfMaterialSearchSelectTag from '@comp/HfManual/HfMaterialSearchSelectTag.vue'

export default {
  name: 'HfSalepriceForm',
  mixins: [HfIODetailMixin],
  components: {
    HfMaterialSearchSelectTag
  },
  watch: {
    'model.price': {
      immediate: true,
      handler(newVal, oldVal) {
        this.model.taxPrice = newVal * ((this.model.tax + 100) / 100)
      }
    }
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        tax: 13
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        customId: [{ required: true, message: '请输入供应商!' }],
        materialCode: [{ required: true, message: '请输入产品!' }],
        useTime: [{ required: true, message: '请输入执行时间!' }],
        price: [{ required: true, message: '请输入未税单价!' }],
        tax: [{ required: true, message: '请输入税率!' }]
      },
      url: {
        add: '/sale/hfSalePrice/add',
        edit: '/sale/hfSalePrice/edit',
        queryById: '/sale/hfSalePrice/queryById',
        materialList: '/material/hfMaterial/queryByMaterialCodeOrName'
      },
      newPrice: false
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    addNewPrice(record) {
      this.model = Object.assign({}, record)
      this.newPrice = true
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              console.log('this.model', this.model)
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.newPrice = false
            })
        }
      })
    },
    onMaterialChange([, record]) {
      Object.assign(this.model, {
        materialId: record.materialId,
        materialCode: record.materialCode,
        materialName: record.materialName,
        sap: record.sap,
        unit: record.unit,
        materialType: record.materialType,
        perNumber: record.perNumber,
        warehouse: record.warehouse
      })
    }
  }
}
</script>
